<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="12">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
          <a-button style="margin-left: 10px" @click="handleRefresh">刷新列表</a-button>
        </a-col>
        <a-col :span="12">
          <a-input-search
            style="max-width: 300px; min-width: 150px;float: right;"
            v-model="queryParam.search"
            placeholder="请输入题库名称"
            @search="onSearch"
          />
        </a-col>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="question_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
      tableLayout="auto"
      :scroll="{x:true}"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
      <div slot="question_name" slot-scope="text, item">
        <a-tag v-if="item.status==0" color="green">正常</a-tag>
        <a-tag v-else color="red">停用</a-tag>
        <span style="margin-right: 6px">{{ text }}</span>
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:topic @click="showTopicList(item)">题目管理</a>
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <a-popconfirm
          v-action:delete
          title="确认删除当前记录？"
          ok-text="确定"
          cancel-text="取消"
          @confirm="handleDelete(item)"
        >
          <a href="#">删除</a>
        </a-popconfirm>
      </div>
    </s-table>
    <SaveForm ref="SaveForm" @handleSubmit="handleRefresh" />
    <TopicListModel ref="TopicListModel" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/question/index'
import { STable } from '@/components'
import SaveForm from './modules/SaveForm'
import TopicListModel from './TopicListModel/Index'

export default {
  name: 'Index',
  components: {
    STable,
    SaveForm,
    TopicListModel
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'no',
          key: 'no',
          customRender: (text, record, index) => `${index + 1}`
        },
        // {
        //   title: '题库ID',
        //   dataIndex: 'question_id'
        // },
        {
          title: '题库名称',
          dataIndex: 'question_name',
          scopedSlots: { customRender: 'question_name' }
        },
        {
          title: '及格分数',
          dataIndex: 'passing_mark'
        },
        {
          title: '考试时长（分钟）',
          dataIndex: 'exam_duration'
        },
        {
          title: '创建时间',
          dataIndex: 'create_time'
        },
        {
          fixed: 'right',
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      },

      selectedRowKeys: []
    }
  },
  created () {
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    showTopicList (item) {
      this.$refs.TopicListModel.open(item)
    },

    /**
     * 新增记录
     */
    handleAdd () {
      this.$refs.SaveForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.SaveForm.edit(item)
    },

    /**
     * 删除记录
     */
    async handleDelete (item) {
      const { message } = await Api.deleted({ apiId: item['question_id'] })
      this.$message.success(message, 1.5)
      this.handleRefresh()

    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch () {
      this.handleRefresh(true)
    }
  }
}
</script>
